﻿using Sc.Domain;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Persistence
{
    public class Car_MaintainDAO
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Car_Maintain model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Car_Maintain(");
            strSql.Append("CarId,DiverId,MtRemark,MtOpinion,MtMoney,MtTime,HasAttachments,State,InstanceState,ApproalUsers,NodeCount,UserId,MtGrade,MtType)");
            strSql.Append(" values (");
            strSql.Append("@CarId,@DiverId,@MtRemark,@MtOpinion,@MtMoney,@MtTime,@HasAttachments,@State,@InstanceState,@ApproalUsers,@NodeCount,@UserId,@MtGrade,@MtType)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@CarId", SqlDbType.Int),
					new SqlParameter("@DiverId", SqlDbType.Int),
					new SqlParameter("@MtRemark", SqlDbType.Text),
					new SqlParameter("@MtOpinion", SqlDbType.NVarChar),
					new SqlParameter("@MtMoney", SqlDbType.Decimal),
					new SqlParameter("@MtTime", SqlDbType.DateTime),
					new SqlParameter("@HasAttachments", SqlDbType.Bit),
					new SqlParameter("@State", SqlDbType.Int),
					new SqlParameter("@InstanceState", SqlDbType.Int),
					new SqlParameter("@ApproalUsers", SqlDbType.NVarChar),
					new SqlParameter("@NodeCount", SqlDbType.Int),
					new SqlParameter("@UserId", SqlDbType.Int),
                    new SqlParameter("@MtGrade", SqlDbType.Int),
                    new SqlParameter("@MtType", SqlDbType.Int)};
            parameters[0].Value = model.CarId;
            parameters[1].Value = model.DiverId;
            parameters[2].Value = model.MtRemark;
            parameters[3].Value = model.MtOpinion;
            parameters[4].Value = model.MtMoney;
            parameters[5].Value = model.MtTime;
            parameters[6].Value = model.HasAttachments;
            parameters[7].Value = model.State;
            parameters[8].Value = model.InstanceState;
            parameters[9].Value = model.ApproalUsers;
            parameters[10].Value = model.NodeCount;
            parameters[11].Value = model.UserId;
            parameters[12].Value = model.MtGrade;
            parameters[13].Value = model.MtType;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 获取详情（单个实体）
        /// </summary>
        /// <param name="Id">Id</param>
        /// <returns></returns>
        public Car_Maintain GetDetail(int Id)
        {
            Car_Maintain result = null;
            string sql = "SELECT * FROM Car_Maintain WHERE Id=@Id";

            SqlParameter parm = new SqlParameter("@Id", SqlDbType.Int, 4);
            parm.Value = Id;

            using (SqlDataReader sdr = DbHelperSQL.ExecuteReader(sql, parm))
            {
                if (sdr.Read())
                {
                    result = new Car_Maintain();
                    result = BuildObjectFromIDataReader(sdr);
                }
                sdr.Close();
            }
            return result;
        }
        /// <summary>
        /// 从IDataReader中读取数据，组装成实体
        /// </summary>
        /// <param name="dr">数据读取接口</param>
        /// <returns>Car_Maintain实体</returns>
        /// <remarks>
        /// </remarks>
        private Car_Maintain BuildObjectFromIDataReader(IDataReader dr)
        {
            try
            {
                Car_Maintain obj = new Car_Maintain();
                obj.Id = (dr["Id"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("Id")));
                obj.CarId = (dr["CarId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("CarId")));
                obj.DiverId = (dr["DiverId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("DiverId")));
                obj.MtRemark = (dr["MtRemark"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("MtRemark")));
                obj.MtOpinion = (dr["MtOpinion"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("MtOpinion")));
                obj.MtMoney = (dr["MtMoney"] == System.DBNull.Value ? 0M : dr.GetDecimal(dr.GetOrdinal("MtMoney")));
                obj.MtTime = (dr["MtTime"] == System.DBNull.Value ? new DateTime(1900, 1, 1) : dr.GetDateTime(dr.GetOrdinal("MtTime")));
                obj.HasAttachments = (dr["HasAttachments"] == System.DBNull.Value ? false : dr.GetBoolean(dr.GetOrdinal("HasAttachments")));
                obj.State = (dr["State"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("State")));
                obj.InstanceState = (dr["InstanceState"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("InstanceState")));
                obj.ApproalUsers = (dr["ApproalUsers"] == System.DBNull.Value ? String.Empty : dr.GetString(dr.GetOrdinal("ApproalUsers")));
                obj.NodeCount = (dr["NodeCount"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("NodeCount")));
                obj.UserId = (dr["UserId"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("UserId")));
                obj.MtGrade = (dr["MtGrade"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("MtGrade")));
                obj.MtType = (dr["MtType"] == System.DBNull.Value ? 0 : dr.GetInt32(dr.GetOrdinal("MtType")));

                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception("从DataReader中读取数据失败，可能与相关的字段不存在有关！请检查！||" + ex.Message + "||" + ex.Source);
            }
        }
        /// <summary>
        /// 更新保险购买申请
        /// </summary>
        /// <param name="ent">业务数据</param>
        /// <returns></returns>
        public int Update(Car_Maintain ent)
        {
            string sql = "UPDATE Car_Maintain SET InstanceState=@InstanceState,ApproalUsers=@ApproalUsers,NodeCount=@NodeCount WHERE Id=@Id";
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@InstanceState",SqlDbType.Int),
                new SqlParameter("@ApproalUsers",SqlDbType.NVarChar),
                new SqlParameter("@NodeCount",SqlDbType.Int),
                new SqlParameter("@Id",SqlDbType.Int)
            };
            parameters[0].Value = ent.InstanceState;
            parameters[1].Value = ent.ApproalUsers;
            parameters[2].Value = ent.NodeCount;
            parameters[3].Value = ent.Id;
            return DbHelperSQL.ExecuteSql(sql, parameters);
        }
    }
}
